
<template>
  <a-modal
      title="详情确认"
      :width="700"
      :open="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }">
      <a-row>
        <a-col :span="12">
          <a-form-item label="物料名称"  name="materialName">
            <a-input  v-model:value="form.materialName" placeholder="物料名称" disabled/>
          </a-form-item>
<!--      <a-form-item label="入库日期"  name="entryDate" >-->
<!--        <a-date-picker style="width: 100%" valueFormat="YYYY-MM-DD" v-model:value="form.entryDate"  placeholder="入库日期" disabled/>-->
<!--      </a-form-item>-->
        </a-col>
        <a-col :span="12">
      <a-form-item label="类别"  name="category">
        <CategoryTree v-model:value="form.category" placeholder="请选择商品分类"
                      :categoryType="CATEGORY_TYPE_ENUM.category.value" disabled/>
      </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
      <a-form-item label="物料代码"  name="materialCode" >
        <a-input  v-model:value="form.materialCode" placeholder="物料代码" disabled/>
      </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="单位" name="unit">
            <a-input  v-model:value="form.unit" placeholder="单位" disabled/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
      <a-form-item label="规格型号"  name="specificationModel" >
        <a-input style="width: 100%" v-model:value="form.specificationModel" placeholder="规格型号" disabled/>
      </a-form-item>
        </a-col>
        <a-col :span="12">
      <a-form-item label="采购数量"  name="purchaseQuantity">
        <a-input-number style="width: 100%" v-model:value="form.purchaseQuantity" placeholder="采购数量" disabled/>
      </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
      <a-form-item label="单价(元)"  name="unitPrice">
        <a-input-number style="width: 100%" v-model:value="form.unitPrice" placeholder="单价（元）" disabled/>
      </a-form-item>
        </a-col>
        <a-col :span="12">
      <a-form-item label="总价(元)"  name="totalPrice">
        <a-input-number style="width: 100%" v-model:value="form.totalPrice" placeholder="总价（元）" disabled/>
      </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
      <a-form-item label="采购途径"  name="purchaseChannel">
        <a-input style="width: 100%" v-model:value="form.purchaseChannel" placeholder="采购途径" disabled/>
      </a-form-item>
        </a-col>
        <a-col :span="12">
      <a-form-item label="存放地点"  name="storageLocation">
        <a-input style="width: 100%" v-model:value="form.storageLocation" placeholder="存放地点" disabled/>
      </a-form-item>
        </a-col>
      </a-row>

      <a-row>
        <a-col :span="12">
      <a-form-item label="采购人"  name="purchaser">
        <!--          <a-input style="width: 100%" v-model:value="form.purchaser" placeholder="采购人" />-->
        <EmployeeSelect ref="employeeSelect" placeholder="采购人员" width="100%" v-model:value="form.purchaser" disabled/>
      </a-form-item>
        </a-col>

          <a-col :span="12">
        <a-form-item label="仓库管理员"  name="warehouseManager">
          <!--          <a-input style="width: 100%" v-model:value="form.warehouseManager" placeholder="仓库管理员" />-->
          <EmployeeSelect ref="employeeSelect" placeholder="请选择仓库管理员" width="100%" v-model:value="form.warehouseManager" />
        </a-form-item>
          </a-col>
      </a-row>

      <a-row>
        <a-col :span="12">
          <a-form-item label="验收人员"  name="inspector" >
            <!--          <a-input style="width: 100%" v-model:value="form.inspector" placeholder="验收人员" />-->
            <EmployeeSelect ref="employeeSelect" placeholder="验收人员" width="100%" v-model:value="form.inspector" disabled/>
          </a-form-item>

        </a-col>
        <a-col :span="12">
          <a-form-item label="备注"  name="remark">
            <a-textarea style="width: 100%" v-model:value="form.remark" placeholder="备注" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-popconfirm
            title="信息确认完毕?"
            ok-text="Yes"
            cancel-text="No"
            @confirm="onSubmit"
        >
          <a-button type="primary" >验收</a-button>
        </a-popconfirm>

      </a-space>
    </template>
  </a-modal>

</template>
<script setup>
import {reactive, ref, nextTick, toRefs, onMounted} from 'vue';
import _ from 'lodash';
import { message } from 'ant-design-vue';
import { SmartLoading } from '/@/components/framework/smart-loading';
import { consumablesInventoryApi } from '/@/api/business/consumables-inventory/consumables-inventory-api';
import { smartSentry } from '/@/lib/smart-sentry';
import EmployeeSelect from '/@/components/system/employee-select/index.vue';
import {useUserStore} from "/@/store/modules/system/user.js";
import {fabricMaterialsApi} from "/@/api/business/fabric-materials/fabric-materials-api.js";
import CategoryTree from '/@/components/business/category-tree-select/index.vue';
// ------------------------ 事件 ------------------------
const CATEGORY_TYPE_ENUM = {
  category: {
    value: 2,
    desc: '类别',
  },
}
const emits = defineEmits(['reloadList']);

const userInfo = useUserStore();

const acceptUser = userInfo.actualName;

// ------------------------ 显示与隐藏 ------------------------
// 是否显示
const visibleFlag = ref(false);

function show(rowData) {
  Object.assign(form, formDefault);
  if (rowData && !_.isEmpty(rowData)) {
    Object.assign(form, rowData);
  }
  form.inspector =  useUserStore().employeeId;

  // console.log('accpetUser',acceptUser)
  // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
  // if (form.status && form.status.length > 0) {
  //   form.status = form.status.map((e) => e.valueCode);
  // }
  visibleFlag.value = true;
  nextTick(() => {
    formRef.value.clearValidate();
  });
}

function onClose() {
  Object.assign(form, formDefault);
  visibleFlag.value = false;
}

onMounted(()=>{

})

// ------------------------ 表单 ------------------------

// 组件ref
const formRef = ref();
const employeeSelect = ref();
const defaultDepartmentForm = {
  managerId: undefined, //部门负责人
};


let formState = reactive({
  ...defaultDepartmentForm,
});


const formDefault = {
  id: undefined,
  entryDate: undefined, //入库日期
  serialNumber: undefined, //序号
  category: undefined, //类别
  materialCode: undefined, //物料代码
  materialName: undefined, //物料名称
  specificationModel: undefined, //规格型号
  purchaseQuantity: undefined, //采购数量
  unitPrice: undefined, //单价（元）
  totalPrice: undefined, //总价（元）
  purchaseChannel: undefined, //采购途径
  storageLocation: undefined, //存放地点
  remark: undefined, //备注
  actualName: undefined,
  mankind: undefined,
  unit: undefined,
  actualAdmin:undefined,
  warehouseManager:undefined,
  purchaser:undefined,
  inspector:undefined,
  warehouseManagerActualName:undefined,
  purchaserActualName:undefined,
  inspectorActualName:undefined,
  status: undefined, //状态
};

let form = reactive({ ...formDefault });






// 点击确定，验证表单
async function onSubmit() {
  try {
    save();
  } catch (err) {
    message.error('参数验证错误，请仔细填写表单数据!');
  }
}

// 新建、编辑API

async function save() {
  SmartLoading.show();
  // const fabricFrom = {
  //   id:form.id,
  //   fabricMaterialCode: form.materialCode, //物料代码
  //   fabricMaterialName: form.materialName, //物料名称
  //   fabicMaterialCategory: form.category, //类别
  //   specModel: form.specificationModel, //规格型号
  //   quantity: form.purchaseQuantity, //采购数量
  //   unit: form.unit, //单位
  //   unitPrice: form.unitPrice, //单价（元）
  //   totalPrice: form.totalPrice, //总价（元）
  //   purchaseWay: form.purchaseChannel, //采购途径
  //   storageLocation: form.storageLocation, //存放地点
  // }
  try {
    // let result;
    // await  fabricMaterialsApi.getCode(fromNew.fabricMaterialCode).then((res)=>{
    //   result = res.data;
    // })
    // if (result == '0'){
    //   await fabricMaterialsApi.add(fromNew);
    // }else if(result == '1'){
    //   await fabricMaterialsApi.update(fromNew);
    // }else {
    //   message.error('物料代码已存在');
    //   return;
    // }
    await consumablesInventoryApi.accept({...form,status:1})
    message.success('操作成功');
    emits('reloadList');
    onClose();
  } catch (err) {
    smartSentry.captureError(err);
  } finally {
    SmartLoading.hide();
  }
}

defineExpose({
  show,
});



</script>
